﻿@page "/category/{channelId}"
@using AdminShared.Models.Article

<Layout Style=" padding: 0 24px 24px;">
    <Breadcrumb Style="margin: 16px 0;">
        <BreadcrumbItem>返回上一页</BreadcrumbItem>
        <BreadcrumbItem>首页</BreadcrumbItem>
        <BreadcrumbItem>栏目管理</BreadcrumbItem>
        <BreadcrumbItem>所有栏目</BreadcrumbItem>
    </Breadcrumb>
    <Content Class="site-layout-background">

        <div style="margin-bottom:10px">
            <Button Icon="plus" Type="@ButtonType.Primary" @onclick="()=>EditCategory()">添加</Button>
        </div>
        <Table TItem="DtoCategory" DataSource="@pageList.List" Total="@pageList.Total" Loading="tableLoading" RemoteDataSource>

            <RowTemplate>
                <Column @bind-Field="@context.Name" Title="名称"></Column>
                <Column @bind-Field="@context.Remarks" Title="备注"></Column>
                <Column @bind-Field="@context.ParentName" Title="父级栏目"></Column>
                <Column @bind-Field="@context.Sort" Title="排序"></Column>
                <Column Field="@context.CreateTime.LocalDateTime" Format="yyyy-MM-dd HH:mm" Title="创建时间"></Column>
                <ActionColumn Title="操作">
                    <Space>
                        <SpaceItem>
                            <a @onclick="()=>EditCategory(context)">编辑</a>
                        </SpaceItem>
                        <SpaceItem>
                            <Popconfirm Title="确认要删除吗？"
                                        OnConfirm="_=>DeleteCategory(context.Id)"
                                        OkText="Yes"
                                        CancelText="No">
                                <a style="color:red">删除</a>
                            </Popconfirm>

                        </SpaceItem>
                    </Space>
                </ActionColumn>
            </RowTemplate>

            <PaginationTemplate>
                <div style="margin:15px 0;float:right">
                    <Pagination Total="@pageList.Total" PageSize="pageSize" Current="pageNum" ShowSizeChanger OnChange="PageChange" ShowQuickJumper ShowTotal="showTotal" />
                </div>
            </PaginationTemplate>

        </Table>

    </Content>
</Layout>


<Drawer Closable="true" Width="380" Visible="isShowEditCategory" Title='("栏目信息")' OnClose="()=>isShowEditCategory=false">
    <Template style="height:90%">

        <Form Model="@editCategory" OnFinish="SaveCategory">

            <FormItem>
                <Text>名称</Text>
                <Input Placeholder="请输入名称" @bind-Value="@context.Name" />
            </FormItem>


            <FormItem>
                <Text>父级栏目</Text>
                <TreeSelect TItem="DtoTreeSelect" Style="width:100%;" DataSource="selectCategoryList" @bind-Value="@parentId" Placeholder="请选择父级栏目" TreeDefaultExpandAll ChildrenExpression="node=>node.DataItem.Children" DisabledExpression="node=>node.DataItem.IsDisabled" TitleExpression="node=>node.DataItem.Title" KeyExpression="node=>node.DataItem.Key" IsLeafExpression="node=>node.DataItem.Children==null"></TreeSelect>
            </FormItem>


            <FormItem>
                <Text>备注</Text>
                <Input Placeholder=" 请输入昵称" @bind-Value="@context.Remarks" />
            </FormItem>


            <FormItem>
                <Text>排序</Text>
                <Input Placeholder="请输入排序值" Type="number" @bind-Value="@context.Sort" />
            </FormItem>


            <Row Gutter="24">
                <AntDesign.Col Span="24">
                    <div style="float:right">
                        <Button Type="@ButtonType.Primary" HtmlType="submit" Loading="@saveLoading">保存</Button>
                        <Button Type="@ButtonType.Default" @onclick="()=>isShowEditCategory=false">取消</Button>
                    </div>
                </AntDesign.Col>
            </Row>
        </Form>
    </Template>
</Drawer>


@code {

    private string parentId = "0";


    [Parameter]
    public string? channelId { get; set; }

    List<DtoTreeSelect>? selectCategoryList = new();

    override protected void OnInitialized()
    {
        GetCategoryTreeList();
    }

    override protected void OnParametersSet()
    {
        GetCategoryList();
        GetCategoryTreeList();
    }


    async void GetCategoryTreeList()
    {
        var parentKVList = new List<DtoKeyValueChild>();


        parentKVList.Add(new DtoKeyValueChild { Key = 0, Value = "无父级栏目" });



        var kvList = await Http.GetFromJsonAsync<List<DtoKeyValueChild>>("Article/GetCategoryTreeList?channelId=" + channelId);

        if (kvList != null)
        {
            parentKVList.AddRange(kvList);

            selectCategoryList = CategoryTreeConvert(parentKVList)!.ToList();

            Console.WriteLine(selectCategoryList);
        }

    }

    private IEnumerable<DtoTreeSelect>? CategoryTreeConvert(List<DtoKeyValueChild>? keyValueChildren)
    {
        return keyValueChildren?.Select(t => new DtoTreeSelect
            {
                Key = t.Key!.ToString()!,
                Title = t.Value!.ToString()!,
                Children = CategoryTreeConvert(t.ChildList),
                IsDisabled = (categoryId.ToString() == t.Key!.ToString() && categoryId.ToString() != "0")
            }).ToList();
    }

    bool saveLoading = false;


    bool tableLoading = false;
    int pageNum = 1;
    int pageSize = 10;
    DtoPageList<DtoCategory> pageList = new();

    async void GetCategoryList()
    {
        tableLoading = true;
        var retData = await Http.GetFromJsonAsync<DtoPageList<DtoCategory>>("Article/GetCategoryList?channelId=" + channelId + "&pageNum=" + pageNum + "&pageSize=" + pageSize);

        if (retData != null)
        {
            pageList = retData;
        }

        tableLoading = false;
        StateHasChanged();
    }

    void PageChange(PaginationEventArgs args)
    {
        if (pageNum != args.Page)
        {
            pageNum = args.Page;
        }

        if (pageSize != args.PageSize)
        {
            pageSize = args.PageSize;
        }

        GetCategoryList();
    }

    Func<PaginationTotalContext, string> showTotal = pageList => $"共 {pageList.Total} 条";


    bool isShowEditCategory = false;
    DtoEditCategory editCategory = new();
    long categoryId;


    async void EditCategory(DtoCategory? category = null)
    {
        editCategory = new();
        categoryId = default;

        GetCategoryTreeList();

        if (category != null)
        {
            categoryId = category.Id;
            editCategory.Name = category.Name;
            editCategory.Remarks = category.Remarks;
            editCategory.Sort = category.Sort;

            if (category.ParentId == null)
            {
                editCategory.ParentId = 0;
            }
            else
            {
                editCategory.ParentId = category.ParentId;
            }

            parentId = editCategory.ParentId.Value.ToString();
        }
        else
        {
            editCategory.Sort = 99;
        }

        await Task.Delay(200);

        isShowEditCategory = true;

        StateHasChanged();
    }

    async void SaveCategory()
    {
        saveLoading = true;

        if (parentId == "0")
        {
            editCategory.ParentId = null;
        }
        else
        {
            editCategory.ParentId = long.Parse(parentId);
        }

        if (categoryId == default)
        {
            editCategory.ChannelId = long.Parse(channelId!);

            using (var httpResponse = await Http.PostAsJsonAsync<DtoEditCategory>("Article/CreateCategory", editCategory))
            {
                var createCategoryId = httpResponse.Content.ReadAsStringAsync().Result;

                Message.Success("添加成功");
            }
        }
        else
        {
            using (var httpResponse = await Http.PostAsJsonAsync<DtoEditCategory>("Article/UpdateCategory?categoryId=" + categoryId, editCategory))
            {
                var updateCategoryRet = httpResponse.Content.ReadAsStringAsync().Result;

                if (bool.Parse(updateCategoryRet))
                {
                    Message.Success("编辑成功");
                }
            }
        }

        saveLoading = false;

        GetCategoryList();
        isShowEditCategory = false;
    }


    async void DeleteCategory(long categoryId)
    {
        using (var httpResponse = await Http.DeleteAsync("Article/DeleteCategory?id=" + categoryId))
        {
            var retValue = httpResponse.Content.ReadAsStringAsync().Result;

            if (Convert.ToBoolean(retValue))
            {
                GetCategoryList();
                Message.Success("删除成功");
            }
        }
    }


}
